package pl.enigmatic.parser.xml.java.util;

import java.util.Locale;

import org.w3c.dom.Element;

import pl.enigmatic.util.Strings;
import pl.enigmatic.util.XML;
import pl.enigmatic.parser.string.java.util.LocaleConverter;
import pl.enigmatic.parser.xml.Parser;
import pl.enigmatic.parser.xml.ParsingInfo;


public class LocaleParser extends Parser<Locale> {

	private final LocaleConverter converter = new LocaleConverter();

	public LocaleParser() {
		super(Locale.class, false);
	}

	@Override
	public Locale parse(final ParsingInfo<Locale> info) {
		Element node = info.getElement();
		String value = XML.getText(node);
		if (Strings.empty(value)) {
			String lang = node.getAttribute("lang");
			String country = node.getAttribute("country");
			String variant = node.getAttribute("variant");
			return converter.convert(new String[] { lang, country, variant });
		} else {
			return converter.convert(value);
		}
	}
}
